from sqlalchemy import Column, String, BigInteger, Integer, DateTime, CHAR, Text, LargeBinary

from app.models.base import BaseModel, TenantMixin, SoftDeleteMixin


class Config(BaseModel, TenantMixin):
    """系统配置模型 - 匹配sys_config表结构"""
    __tablename__ = "sys_config"

    config_id = Column(BigInteger, primary_key=True, comment="参数主键")
    config_name = Column(String(100), default='', comment="参数名称")
    config_key = Column(String(100), default='', comment="参数键名")
    config_value = Column(String(500), default='', comment="参数键值")
    config_type = Column(CHAR(1), default='N', comment="系统内置（Y是 N否）")


class DictType(BaseModel, TenantMixin):
    """字典类型模型 - 匹配sys_dict_type表结构"""
    __tablename__ = "sys_dict_type"

    dict_id = Column(BigInteger, primary_key=True, comment="字典主键")
    dict_name = Column(String(100), default='', comment="字典名称")
    dict_type = Column(String(100), default='', comment="字典类型")


class DictData(BaseModel, TenantMixin):
    """字典数据模型 - 匹配sys_dict_data表结构"""
    __tablename__ = "sys_dict_data"

    dict_code = Column(BigInteger, primary_key=True, comment="字典编码")
    dict_sort = Column(Integer, default=0, comment="字典排序")
    dict_label = Column(String(100), default='', comment="字典标签")
    dict_value = Column(String(100), default='', comment="字典键值")
    dict_type = Column(String(100), default='', comment="字典类型")
    css_class = Column(String(100), comment="样式属性（其他样式扩展）")
    list_class = Column(String(100), comment="表格回显样式")
    is_default = Column(CHAR(1), default='N', comment="是否默认（Y是 N否）")


class Notice(BaseModel, TenantMixin):
    """通知公告模型 - 匹配sys_notice表结构"""
    __tablename__ = "sys_notice"

    notice_id = Column(BigInteger, primary_key=True, comment="公告ID")
    notice_title = Column(String(50), nullable=False, comment="公告标题")
    notice_type = Column(CHAR(1), nullable=False, comment="公告类型（1通知 2公告）")
    notice_content = Column(LargeBinary, comment="公告内容")
    status = Column(CHAR(1), default='0', comment="公告状态（0正常 1关闭）")


class OperLog(BaseModel, TenantMixin):
    """操作日志模型 - 匹配sys_oper_log表结构"""
    __tablename__ = "sys_oper_log"

    oper_id = Column(BigInteger, primary_key=True, comment="日志主键")
    title = Column(String(50), default='', comment="模块标题")
    business_type = Column(Integer, default=0, comment="业务类型（0其它 1新增 2修改 3删除）")
    method = Column(String(100), default='', comment="方法名称")
    request_method = Column(String(10), default='', comment="请求方式")
    operator_type = Column(Integer, default=0, comment="操作类别（0其它 1后台用户 2手机端用户）")
    oper_name = Column(String(50), default='', comment="操作人员")
    dept_name = Column(String(50), default='', comment="部门名称")
    oper_url = Column(String(255), default='', comment="请求URL")
    oper_ip = Column(String(128), default='', comment="主机地址")
    oper_location = Column(String(255), default='', comment="操作地点")
    oper_param = Column(String(4000), default='', comment="请求参数")
    json_result = Column(String(4000), default='', comment="返回参数")
    status = Column(Integer, default=0, comment="操作状态（0正常 1异常）")
    error_msg = Column(String(4000), default='', comment="错误消息")
    oper_time = Column(DateTime, comment="操作时间")
    cost_time = Column(BigInteger, default=0, comment="消耗时间")


class LoginLog(BaseModel, TenantMixin):
    """登录日志模型 - 匹配sys_logininfor表结构"""
    __tablename__ = "sys_logininfor"

    info_id = Column(BigInteger, primary_key=True, comment="访问ID")
    user_name = Column(String(50), default='', comment="用户账号")
    client_key = Column(String(32), default='', comment="客户端")
    device_type = Column(String(32), default='', comment="设备类型")
    ipaddr = Column(String(128), default='', comment="登录IP地址")
    login_location = Column(String(255), default='', comment="登录地点")
    browser = Column(String(50), default='', comment="浏览器类型")
    os = Column(String(50), default='', comment="操作系统")
    status = Column(CHAR(1), default='0', comment="登录状态（0成功 1失败）")
    msg = Column(String(255), default='', comment="提示消息")
    login_time = Column(DateTime, comment="访问时间")


class Oss(BaseModel, TenantMixin):
    """对象存储模型 - 匹配sys_oss表结构"""
    __tablename__ = "sys_oss"

    oss_id = Column(BigInteger, primary_key=True, comment="对象存储主键")
    file_name = Column(String(255), nullable=False, default='', comment="文件名")
    original_name = Column(String(255), nullable=False, default='', comment="原名")
    file_suffix = Column(String(10), nullable=False, default='', comment="文件后缀名")
    url = Column(String(500), nullable=False, comment="URL地址")
    service = Column(String(20), nullable=False, default='minio', comment="服务商")


class OssConfig(BaseModel, TenantMixin):
    """对象存储配置模型 - 匹配sys_oss_config表结构"""
    __tablename__ = "sys_oss_config"

    oss_config_id = Column(BigInteger, primary_key=True, comment="主键")
    config_key = Column(String(20), nullable=False, default='', comment="配置key")
    access_key = Column(String(255), default='', comment="accessKey")
    secret_key = Column(String(255), default='', comment="秘钥")
    bucket_name = Column(String(255), default='', comment="桶名称")
    prefix = Column(String(255), default='', comment="前缀")
    endpoint = Column(String(255), default='', comment="访问站点")
    domain = Column(String(255), default='', comment="自定义域名")
    is_https = Column(CHAR(1), default='N', comment="是否https（Y=是,N=否）")
    region = Column(String(255), default='', comment="域")
    access_policy = Column(CHAR(1), nullable=False, default='1', comment="桶权限类型(0=private 1=public 2=custom)")
    status = Column(CHAR(1), default='1', comment="是否默认（0=是,1=否）")
    ext1 = Column(String(255), default='', comment="扩展字段")


class Social(BaseModel, TenantMixin):
    """第三方认证模型 - 匹配sys_social表结构"""
    __tablename__ = "sys_social"

    id = Column(BigInteger, primary_key=True, comment="主键")
    user_id = Column(BigInteger, nullable=False, comment="用户ID")
    auth_id = Column(String(255), nullable=False, comment="平台+平台唯一id")
    source = Column(String(255), nullable=False, comment="用户来源")
    open_id = Column(String(255), comment="平台编号唯一id")
    user_name = Column(String(30), nullable=False, comment="登录账号")
    nick_name = Column(String(30), default='', comment="用户昵称")
    email = Column(String(255), default='', comment="用户邮箱")
    avatar = Column(String(500), default='', comment="头像地址")
    access_token = Column(String(255), nullable=False, comment="用户的授权令牌")
    expire_in = Column(Integer, comment="用户的授权令牌的有效期，部分平台可能没有")
    refresh_token = Column(String(255), comment="刷新令牌，部分平台可能没有")
    access_code = Column(String(255), comment="平台的授权信息，部分平台可能没有")


class Client(BaseModel, SoftDeleteMixin):
    """客户端配置模型 - 匹配sys_client表结构"""
    __tablename__ = "sys_client"

    id = Column(BigInteger, primary_key=True, comment="id")
    client_id = Column(String(64), comment="客户端id")
    client_key = Column(String(32), comment="客户端key")
    client_secret = Column(String(255), comment="客户端秘钥")
    grant_type = Column(String(255), comment="授权类型")
    device_type = Column(String(32), comment="设备类型")
    active_timeout = Column(Integer, default=1800, comment="token活跃超时时间")
    timeout = Column(Integer, default=604800, comment="token固定超时")
    status = Column(CHAR(1), default='0', comment="状态（0正常 1停用）")